package com.easymall.web;

import com.easymall.domain.Prod;
import com.easymall.service.ProdService;
import com.easymall.util.BaseFactory;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

@WebServlet(name = "ProdManageServlet")
public class ProdManageServlet extends HttpServlet {

    private ProdService prodService;

    @Override
    public void init() throws ServletException {
        try {
            prodService = BaseFactory.getFactory().getInstance(ProdService.class);
        } catch (Exception e) {
            e.printStackTrace();
            throw new ServletException(e.getMessage());
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            List<Prod> prodList = prodService.listProdAll();
            request.setAttribute("prodList", prodList);
            request.getRequestDispatcher("_prod_manage.jsp").forward(request, response);
        } catch (SQLException e) {
            e.printStackTrace();
            response.sendError(500);
        }
    }
}
